<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">




  <meta name="google-site-verification" content="A_78WZOvfJTnffhkbRiXvoT0JZXLHHpQQ1lN0GVkPBU">









  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.2.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.2.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.2.0">


  <link rel="mask-icon" href="/images/logo.svg?v=7.2.0" color="#222">






<link rel="stylesheet" href="/css/main.css?v=7.2.0">






<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">








<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.2.0',
    sidebar: {"position":"left","display":"always","offset":12,"onmobile":true},
    back2top: {"enable":true,"sidebar":true,"scrollpercent":true},
    copycode: {"enable":false,"show_result":false,"style":null},
    fancybox: false,
    mediumzoom: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    }
  };
</script>

  <meta name="keywords" content="echototo666">
<meta property="og:type" content="website">
<meta property="og:title" content="echototo6666">
<meta property="og:url" content="http://www.echototo666.com/page/5/index.html">
<meta property="og:site_name" content="echototo6666">
<meta property="og:locale" content="zh-CN">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="echototo6666">



  <link rel="alternate" href="/atom.xml" title="echototo6666" type="application/atom+xml">



  
  
  <link rel="canonical" href="http://www.echototo666.com/page/5/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  
  <title>echototo6666 - 站在巨人肩膀上，注定也要成为巨人的一部分</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">echototo6666</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">站在巨人肩膀上，注定也要成为巨人的一部分</h1>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
      
    

    
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
      
    

    
      
    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类<span class="badge">6</span></a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
      
    

    
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档<span class="badge">35</span></a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



</div>
    </header>

    
  
  

  

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dkeTMzMTY0NDc0MQ==" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewbox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></span>



    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.echototo666.com/2018/04/27/laravel/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Lip">
      <meta itemprop="description" content>
      <meta itemprop="image" content="https://avatars0.githubusercontent.com/u/9856659">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="echototo6666">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2018/04/27/laravel/" class="post-title-link" itemprop="url">通过laravel了解DI/IoC</a>
              
            
          </h2>
        

        <div class="post-meta">
          

          
          
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-04-27 23:00:34" itemprop="dateCreated datePublished" datetime="2018-04-27T23:00:34+00:00">2018-04-27</time>
            </span>
          

          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2020-03-24 02:40:06" itemprop="dateModified" datetime="2020-03-24T02:40:06+00:00">2020-03-24</time>
              </span>
            
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/php/" itemprop="url" rel="index"><span itemprop="name">php</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          <br>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <blockquote>
<p>高级模块不应该依赖于低级模块，两者都应该依赖于抽象对象。 抽象不应该依赖于细节，细节应该取决于抽象。<br>—Robert C. Martin</p>
</blockquote>
<blockquote>
<p>「ioc 依赖注入」的结果就是「DI 控制反转」的目的，也就说 控制反转 的最终目标是为了 实现项目的高内聚低耦合，而 实现这种目标 的方式则是通过 依赖注入。 </p>
</blockquote>
<p><em>说白了  DI/ioc 其实是抽象工厂模式的一种升华(超级工厂)。</em></p>
<h2 id="一、依赖的产生"><a href="#一、依赖的产生" class="headerlink" title="一、依赖的产生"></a>一、依赖的产生</h2><p>直接上代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">interface interfaceB&#123;&#125;</span><br><span class="line">class B implements interfaceB</span><br><span class="line">&#123;</span><br><span class="line">	public function someFun()&#123;</span><br><span class="line">		echo &quot;DI/IoC&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class A</span><br><span class="line">&#123;</span><br><span class="line">	protected $a; //放入B的实例</span><br><span class="line">	public function __construct(interfaceB $objectB)&#123; //类型提示类(面向接口)</span><br><span class="line">		$this-&gt;a = $objectB;</span><br><span class="line">	&#125;</span><br><span class="line">	public function aFun()&#123;</span><br><span class="line">		$this-&gt;a-&gt;someFun();//调用B类的方法</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$bb = new B();</span><br><span class="line">$a = new A($bb);</span><br><span class="line">$a-&gt;aFun();</span><br></pre></td></tr></table></figure>
<h2 id="二、IoC容器"><a href="#二、IoC容器" class="headerlink" title="二、IoC容器"></a>二、IoC容器</h2><p>上面的例子我们是通过手动的方式注入依赖，而IoC可以自动实现依赖注入。下面通过laravel框架中的设计方法进行简化。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line">class Container</span><br><span class="line">&#123;</span><br><span class="line">	protected $bindings = [];</span><br><span class="line"></span><br><span class="line">	//2.1绑定*************************</span><br><span class="line">	public function bind($abstract, $concrete=null, $shared = false)&#123;</span><br><span class="line"></span><br><span class="line">		//如果不是回调函数，则产生一个回调函数</span><br><span class="line">		if(! $concrete instanceof Closure)&#123;</span><br><span class="line">			$concrete = $this-&gt;getClosure($abstract,$concrete);</span><br><span class="line">		&#125;</span><br><span class="line">		$this-&gt;bindings[$abstract] = compact(&apos;concrete&apos;,&apos;share&apos;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	//默认生成的回调函数</span><br><span class="line">	protected function getClosure($abstract, $concrete)&#123;</span><br><span class="line">		return function($c) use($abstract, $concrete)&#123;</span><br><span class="line">			$method = ($abstract == $concrete)?&apos;build&apos;:&apos;make&apos;;</span><br><span class="line">			return $c-&gt;$method($concrete);</span><br><span class="line">		&#125;;</span><br><span class="line">	&#125;</span><br><span class="line">	//*************************************</span><br><span class="line">	//2.2解析-解决依赖</span><br><span class="line">	public function make($abstract)&#123;</span><br><span class="line">		$concrete = isset($this-&gt;bindings[$abstract])? $this-&gt;bindings[$abstract][&apos;concrete&apos;] : $abstract;</span><br><span class="line"></span><br><span class="line">		if($this-&gt;isBuildable($concrete,$abstract))&#123;</span><br><span class="line">			$object = $this-&gt;build($concrete);</span><br><span class="line">		&#125;else&#123;</span><br><span class="line">			$object = $this-&gt;make($concrete);</span><br><span class="line">		&#125;</span><br><span class="line">		return $object;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	protected function isBuildable($concrete, $abstract)&#123;</span><br><span class="line">		return $concrete === $abstract || $concrete instanceof Closure;</span><br><span class="line">	&#125;</span><br><span class="line">	//2.3实例*****************************************</span><br><span class="line">	public function build($concrete)&#123;</span><br><span class="line">		if ($concrete instanceof Closure) &#123;</span><br><span class="line">			return $concrete($this);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		$reflector = new ReflectionClass($concrete);</span><br><span class="line"></span><br><span class="line">		if (! $reflector-&gt;isInstantiable()) &#123;</span><br><span class="line">			echo $message = &quot;Target [$concrete] is not instantiable.&quot;;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		$constructor = $reflector-&gt;getConstructor();//获取类的构造函数</span><br><span class="line">		if (is_null($constructor)) &#123;</span><br><span class="line">			return new $concrete;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		$dependencies = $constructor-&gt;getParameters();//构造函数中的参数</span><br><span class="line">		$instances = $this-&gt;getDependencies($dependencies);</span><br><span class="line"></span><br><span class="line">		return $reflector-&gt;newInstanceArgs($instances);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	//解决构造函数参数中存在依赖，则继续解析依赖</span><br><span class="line">	protected function getDependencies($parameters)</span><br><span class="line">	&#123;</span><br><span class="line">		$dependencies = [];</span><br><span class="line"></span><br><span class="line">		foreach ($parameters as $parameter) &#123;</span><br><span class="line">			$dependency = $parameter-&gt;getClass();</span><br><span class="line"></span><br><span class="line">			if(is_null($dependency)) &#123;</span><br><span class="line">				$dependencies[] = null;</span><br><span class="line">			&#125;else&#123;</span><br><span class="line">				$dependencies[] = $this-&gt;resolveClass($parameter);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		return $dependencies;</span><br><span class="line">	&#125;</span><br><span class="line">	protected function resolveClass(ReflectionParameter $parameter)</span><br><span class="line">	&#123;</span><br><span class="line">		return $this-&gt;make($parameter-&gt;getClass()-&gt;name);//获得类型提示类</span><br><span class="line">	&#125;</span><br><span class="line">	//************************************************************</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<hr>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$app = new Container();</span><br><span class="line">$app-&gt;bind(&apos;interfaceB&apos;,&apos;B&apos;);</span><br><span class="line">$app-&gt;bind(&apos;aTestObj&apos;,&apos;A&apos;);</span><br><span class="line"></span><br><span class="line">$aTestObj = $app-&gt;make(&apos;aTestObj&apos;);</span><br><span class="line">$aTestObj-&gt;aFun();</span><br></pre></td></tr></table></figure>
<h2 id="三、超级工厂"><a href="#三、超级工厂" class="headerlink" title="三、超级工厂"></a>三、超级工厂</h2><blockquote>
<p>工厂模式以及抽象工厂模式大家应该都有了解，为什么说ioc是超级工厂呢。假如上面的例子拓展一下。实例A这时不要依赖B类了，要用C类。</p>
</blockquote>
<p>_ 实际的场景如（用烂了的例子），工厂生产汽车。之前用的是德国牌子的TUV轮毂，现在要用日本牌子的轮毂。当然同样都是轮子，因为材料、重量 甚至尺寸以及功能有出入。如果在原用的基础上改甚是麻烦，这时只需重新再写一个类C，而不动A类 _</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">class C implements interfaceB</span><br><span class="line">&#123;</span><br><span class="line">    protected $c = &apos;日本&apos;;</span><br><span class="line">	public function someFun()&#123;</span><br><span class="line">		echo $this-&gt;c;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//修改绑定关系</span><br><span class="line">$app = new Container();</span><br><span class="line">$app-&gt;bind(&apos;interfaceB&apos;,&apos;C&apos;);//达到DI控制反转目的</span><br><span class="line">$app-&gt;bind(&apos;aTestObj&apos;,&apos;A&apos;);</span><br><span class="line"></span><br><span class="line">$aTestObj = $app-&gt;make(&apos;aTestObj&apos;);</span><br><span class="line">$aTestObj-&gt;aFun();</span><br></pre></td></tr></table></figure>
<h2 id="四、容器"><a href="#四、容器" class="headerlink" title="四、容器"></a>四、容器</h2><p>对服务容器进行一个理解：</p>
<blockquote>
<p>容器就是一个装东西的，好比碗。而服务就是这个碗要装的饭呀，菜呀，等等东西。当我们需要饭时，我们就能从这个碗里拿到。如果你想在饭里加点菜（也就是饭依赖注入了菜），我们从碗里直接拿饭就可以了，而这些依赖都由容器解决了（这也就是控制反转）。</p>
</blockquote>
<p>我们需要做的就是对提供的服务进行维护。</p>

          
        
      
    </div>

    

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.echototo666.com/2018/04/27/configure/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Lip">
      <meta itemprop="description" content>
      <meta itemprop="image" content="https://avatars0.githubusercontent.com/u/9856659">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="echototo6666">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2018/04/27/configure/" class="post-title-link" itemprop="url">C configure配置</a>
              
            
          </h2>
        

        <div class="post-meta">
          

          
          
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-04-27 23:00:34" itemprop="dateCreated datePublished" datetime="2018-04-27T23:00:34+00:00">2018-04-27</time>
            </span>
          

          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2020-03-24 02:40:06" itemprop="dateModified" datetime="2020-03-24T02:40:06+00:00">2020-03-24</time>
              </span>
            
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/C/" itemprop="url" rel="index"><span itemprop="name">C</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          <br>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="1-说明"><a href="#1-说明" class="headerlink" title="1 说明"></a>1 说明</h2><blockquote>
<p>在linux 中，经常需要用到交叉编译。简单地说，就是在一个平台上生成另一个平台上的可执行代码。同一个体系结构可以运行不同的操作系统；同样，同一个操作系统也可以在不同的体系结构上运行。<br>对于大部分代码，都有configure文件，让开发者进行配置，配置完毕之后自动生成makefile，然后进行编译。本文旨在说明configure中<br>常用的一些参数。</p>
</blockquote>
<p><span id="configure参数说明"></span></p>
<h2 id="2-configure参数说明"><a href="#2-configure参数说明" class="headerlink" title="2 configure参数说明"></a>2 configure参数说明</h2><p><span id="查看configure 配置选项"></span></p>
<h3 id="2-1-查看configure-配置选项"><a href="#2-1-查看configure-配置选项" class="headerlink" title="2.1 查看configure 配置选项"></a>2.1 查看configure 配置选项</h3><blockquote>
<p>在configure目录下，运行 –help命令，可以查看到configure的配置参数一共有哪些。</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">[02:34:58][root@wdy:~/lnmp1.5/src/php-7.3.6]# ./configure --help</span><br><span class="line">`configure&apos; configures this package to adapt to many kinds of systems.</span><br><span class="line"></span><br><span class="line">Usage: ./configure [OPTION]... [VAR=VALUE]...</span><br><span class="line"></span><br><span class="line">To assign environment variables (e.g., CC, CFLAGS...), specify them as</span><br><span class="line">VAR=VALUE.  See below for descriptions of some of the useful variables.</span><br><span class="line"></span><br><span class="line">Defaults for the options are specified in brackets.</span><br><span class="line"></span><br><span class="line">Configuration:</span><br><span class="line">  -h, --help              display this help and exit</span><br><span class="line">      --help=short        display options specific to this package</span><br><span class="line">      --help=recursive    display the short help of all the included packages</span><br><span class="line">  -V, --version           display version information and exit</span><br><span class="line">  -q, --quiet, --silent   do not print `checking ...&apos; messages</span><br><span class="line">      --cache-file=FILE   cache test results in FILE [disabled]</span><br><span class="line">  -C, --config-cache      alias for `--cache-file=config.cache&apos;</span><br><span class="line">  -n, --no-create         do not create output files</span><br><span class="line">      --srcdir=DIR        find the sources in DIR [configure dir or `..&apos;]</span><br><span class="line"></span><br><span class="line">Installation directories:</span><br><span class="line">  --prefix=PREFIX         install architecture-independent files in PREFIX</span><br><span class="line">                          [/usr/local]</span><br><span class="line">  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX</span><br><span class="line">                          [PREFIX]</span><br><span class="line"></span><br><span class="line">By default, `make install&apos; will install all the files in</span><br><span class="line">`/usr/local/bin&apos;, `/usr/local/lib&apos; etc.  You can specify</span><br><span class="line">an installation prefix other than `/usr/local&apos; using `--prefix&apos;,</span><br><span class="line">for instance `--prefix=$HOME&apos;.</span><br><span class="line"></span><br><span class="line">For better control, use the options below.</span><br><span class="line"></span><br><span class="line">Fine tuning of the installation directories:</span><br><span class="line">  --bindir=DIR            user executables [EPREFIX/bin]</span><br><span class="line">  --sbindir=DIR           system admin executables [EPREFIX/sbin]</span><br></pre></td></tr></table></figure>
<p><span id="2.2参数说明"></span></p>
<h3 id="2-2-参数说明"><a href="#2-2-参数说明" class="headerlink" title="2.2 参数说明"></a>2.2 参数说明</h3><p><span id="2.2.1"></span></p>
<h4 id="2-2-1-build-参数"><a href="#2-2-1-build-参数" class="headerlink" title="2.2.1 build 参数"></a>2.2.1 build 参数</h4><blockquote>
<p> –build=BUILD     configure for building on BUILD [guessed]</p>
</blockquote>
<p>build: 执行代码编译的主机，正常的话就是你的主机系统。这个参数一般由config.guess来猜就可以。当然自己指定也可以。可以默认不写，默认为当前正在使用的ubuntu主机，如 i386-linux</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">--build=i386-linux</span><br><span class="line">或者xilinx-arm的编译器主机</span><br><span class="line">--build=i686-pc-linux-gnu</span><br><span class="line">或者不写</span><br></pre></td></tr></table></figure>
<p><span id="2.2.2"></span></p>
<h5 id="2-2-2-host-参数"><a href="#2-2-2-host-参数" class="headerlink" title="2.2.2 host 参数"></a>2.2.2 host 参数</h5><blockquote>
<p> –host=HOST       cross-compile to build programs to run on HOST [BUILD]</p>
</blockquote>
<p>指定软件运行的系统平台.如果没有指定,将会运行`config.guess’来检测.–host 指定的是交叉编译工具链的前缀。如本位中采用的是arm-xilinx-linux-gnueabi 工具链，则参数配置为:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">--host=arm-xilinx-linux-gnueabi</span><br></pre></td></tr></table></figure>
<p><span id="2.2.3"></span></p>
<h5 id="2-2-3-target-参数"><a href="#2-2-3-target-参数" class="headerlink" title="2.2.3 target 参数"></a>2.2.3 target 参数</h5><blockquote>
<p>  –target=TARGET   configure for building compilers for TARGET [HOST]</p>
</blockquote>
<p>target: 这个选项只有在建立交叉编译环境的时候用到，正常编译和交叉编译都不会用到。他用build主机上的编译器，编译一个新的编译器（binutils, gcc,gdb等），这个新的编译器将来编译出来的其他程序将运行在target指定的系统上。<br>如果不编译新的编译器，这个参数可以不填，或者与 host的参数一致</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">--target=arm-xilinx-linux-gnueabi</span><br><span class="line">或者不写 --target的参数</span><br></pre></td></tr></table></figure>
<p><span id="2.2.4"></span></p>
<h5 id="2-2-4-CC-编译器参数"><a href="#2-2-4-CC-编译器参数" class="headerlink" title="2.2.4 CC 编译器参数"></a>2.2.4 CC 编译器参数</h5><blockquote>
<p>CC          C compiler command</p>
</blockquote>
<p>指定GCC 交叉编译器命令，如果配置了，则使用CC配置的编译器，如果不配置则默认为host对应的GCC工具<br>如配置了 –host=arm-xilinx-linux-gnueabi，则默认CC的编译器为 arm-xilinx-linux-gnueabi-gcc<br>这个参数如无特殊指定，可以忽略不写。</p>
<p><span id="2.2.5"></span></p>
<h5 id="2-2-5-prefix-安装参数"><a href="#2-2-5-prefix-安装参数" class="headerlink" title="2.2.5 prefix 安装参数"></a>2.2.5 prefix 安装参数</h5><blockquote>
<p> –program-prefix=PREFIX            prepend PREFIX to installed program names</p>
</blockquote>
<p>该参数指定编译后，文件安装的目录。<br>不指定prefix，则可执行文件默认放在/usr/local/bin，库文件默认放在/usr/local/lib，配置文件默认放在/usr/local/etc。其他的资源文件放在/usr/local/share。你要卸载这个程序，要么在原来make目录下用make uninstall(前提是make文件指定过make uninstall)，要么去上述文件中一个一个手动删除。如需指定make install 的目录，如 /home/tmp/test</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">--prefix=/home/tmp/test</span><br></pre></td></tr></table></figure>
<p>【转载】<span class="exturl" data-url="aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hob3VmZWkyMDEwL2FydGljbGUvZGV0YWlscy84Mjc2ODk5NQ==" title="https://blog.csdn.net/xhoufei2010/article/details/82768995">https://blog.csdn.net/xhoufei2010/article/details/82768995<i class="fa fa-external-link"></i></span> </p>

          
        
      
    </div>

    

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.echototo666.com/2018/04/27/react_m/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Lip">
      <meta itemprop="description" content>
      <meta itemprop="image" content="https://avatars0.githubusercontent.com/u/9856659">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="echototo6666">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2018/04/27/react_m/" class="post-title-link" itemprop="url">react组件生命周期</a>
              
            
          </h2>
        

        <div class="post-meta">
          

          
          
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-04-27 23:00:34" itemprop="dateCreated datePublished" datetime="2018-04-27T23:00:34+00:00">2018-04-27</time>
            </span>
          

          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2020-03-24 02:40:06" itemprop="dateModified" datetime="2020-03-24T02:40:06+00:00">2020-03-24</time>
              </span>
            
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/前端/" itemprop="url" rel="index"><span itemprop="name">前端</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          <br>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="UI-render-state"><a href="#UI-render-state" class="headerlink" title="UI=render(state)"></a>UI=render(state)</h2><h3 id="react生命周期"><a href="#react生命周期" class="headerlink" title="react生命周期"></a>react生命周期</h3><h2 id="1-装载"><a href="#1-装载" class="headerlink" title="1. 装载"></a>1. 装载</h2><ul>
<li><strong>constructor</strong>构造</li>
<li><strong>getInitialState</strong> 同构造（React.createClass） 弃用</li>
<li><strong>getDefaultProps</strong> 同构造（React.createClass） 弃用</li>
<li><strong>componentWillMount</strong>()  在render之前 这里的东西都可以放在构造里面</li>
<li>render 纯函数</li>
<li><strong>componentDidMount</strong>() 在render之后 当所有的render完成之后 。在客户端浏览器上面全部执行（可用其他UI库jquery）</li>
</ul>
<h2 id="2-更新-当用户交互页面时，通过修改state和props"><a href="#2-更新-当用户交互页面时，通过修改state和props" class="headerlink" title="2. 更新 -当用户交互页面时，通过修改state和props"></a>2. 更新 -当用户交互页面时，通过修改state和props</h2><ul>
<li><p>componentWillReceiveProps(nextProps);</p>
<blockquote>
<p>当父 组件的render 函数被调用， 在 render函数 里面被渲染 的子组件就会经历 更新过程， 不管父组件传给子组件 的 props 有没有改变， 都会 触发 子组件的componentWillReceiveProps函数。</p>
</blockquote>
</li>
<li><p>shouldComponentUpdate(nextProps, nextState){return true;} 在setState  之前执行。</p>
<blockquote>
<p>在更新过程中， React 库首先调用 shouldComponentUpdate 函数， 如果这个函数返回true， 那就会继续更新 过程， 接下来 调用render 函数； 反之， 如果得到 一个 false， 那就立刻停止更新过程.</p>
</blockquote>
</li>
<li><p>当上面函数返回true时。依次调用：<br>  componentWillUpdate<br>  render<br>  componentDidUpdate</p>
</li>
</ul>
<h2 id="3-卸载"><a href="#3-卸载" class="headerlink" title="3. 卸载"></a>3. 卸载</h2><ul>
<li>componentWillUnmount</li>
<li>要配合componentDidMount 使用。要不然会出现内存泄漏</li>
</ul>
<blockquote>
<p>tip:</p>
</blockquote>
<ul>
<li style="list-style: none"><input type="checkbox"> forceUpdate()；刷新render</li>
</ul>
<p><img src="https://user-images.githubusercontent.com/9856659/51670011-a8777280-2000-11e9-96ba-447a3770872f.png" alt="image"></p>

          
        
      
    </div>

    

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.echototo666.com/2018/04/27/remote_debug/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Lip">
      <meta itemprop="description" content>
      <meta itemprop="image" content="https://avatars0.githubusercontent.com/u/9856659">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="echototo6666">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2018/04/27/remote_debug/" class="post-title-link" itemprop="url">远程调试Debug配置</a>
              
            
          </h2>
        

        <div class="post-meta">
          

          
          
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-04-27 23:00:34" itemprop="dateCreated datePublished" datetime="2018-04-27T23:00:34+00:00">2018-04-27</time>
            </span>
          

          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2020-03-24 02:40:06" itemprop="dateModified" datetime="2020-03-24T02:40:06+00:00">2020-03-24</time>
              </span>
            
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/php/" itemprop="url" rel="index"><span itemprop="name">php</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          <br>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h4 id="1-服务器上安装xdebug，参考https-xdebug-org-wizard-php"><a href="#1-服务器上安装xdebug，参考https-xdebug-org-wizard-php" class="headerlink" title="1.服务器上安装xdebug，参考https://xdebug.org/wizard.php"></a>1.服务器上安装xdebug，参考<span class="exturl" data-url="aHR0cHM6Ly94ZGVidWcub3JnL3dpemFyZC5waHA=" title="https://xdebug.org/wizard.php">https://xdebug.org/wizard.php<i class="fa fa-external-link"></i></span></h4><p><img src="https://user-images.githubusercontent.com/9856659/60659778-d05ce300-9e88-11e9-8216-e37a01047b82.png" alt="image"></p>
<h4 id="2-添加server上面php-ini配置"><a href="#2-添加server上面php-ini配置" class="headerlink" title="2.添加server上面php.ini配置"></a>2.添加server上面php.ini配置</h4><p><img src="https://user-images.githubusercontent.com/9856659/60659867-0c904380-9e89-11e9-9d9c-6754647e40d9.png" alt="image"></p>
<blockquote>
<p>其中xdebug.remote_host 是本地IDE的ip<br>xdebug.idekey 要和后面IDE中的配置一致<br>xdebug.remote_handler=dbgp 代理模式</p>
</blockquote>
<h4 id="3-phpstorm配置"><a href="#3-phpstorm配置" class="headerlink" title="3.phpstorm配置"></a>3.phpstorm配置</h4><p><img src="https://user-images.githubusercontent.com/9856659/60659924-26ca2180-9e89-11e9-9c65-3073eddcb97c.png" alt="image"></p>
<blockquote>
<p>代理配置：IDE key  和服务器上面php.ini一致</p>
</blockquote>
<p><img src="https://user-images.githubusercontent.com/9856659/60659943-30538980-9e89-11e9-8c56-e2733eb66ead.png" alt="image"></p>
<h4 id="4-debug配置"><a href="#4-debug配置" class="headerlink" title="4.debug配置"></a>4.debug配置</h4><p><img src="https://user-images.githubusercontent.com/9856659/60659959-3ba6b500-9e89-11e9-8de1-310eec41f551.png" alt="image"></p>

          
        
      
    </div>

    

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.echototo666.com/2018/04/27/dev/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Lip">
      <meta itemprop="description" content>
      <meta itemprop="image" content="https://avatars0.githubusercontent.com/u/9856659">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="echototo6666">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2018/04/27/dev/" class="post-title-link" itemprop="url">设计模式-装饰着模式</a>
              
            
          </h2>
        

        <div class="post-meta">
          

          
          
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-04-27 23:00:34" itemprop="dateCreated datePublished" datetime="2018-04-27T23:00:34+00:00">2018-04-27</time>
            </span>
          

          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2020-03-24 02:40:06" itemprop="dateModified" datetime="2020-03-24T02:40:06+00:00">2020-03-24</time>
              </span>
            
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/php/" itemprop="url" rel="index"><span itemprop="name">php</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          <br>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p><strong>装饰者模式，动态地将责任附加到对象上。若要扩展功能，装饰者提供了比继承更加有弹性的替代方案。</strong></p>
<h3 id="组合和继承的区别"><a href="#组合和继承的区别" class="headerlink" title="组合和继承的区别"></a>组合和继承的区别</h3><ul>
<li>继承</li>
</ul>
<blockquote>
<p>继承是给一个类添加行为的比较有效的途径。通过使用继承，可以使得子类在拥有自身方法的同时，还可以拥有父类的方法。但是使用继承是静态的，在编译的时候就已经决定了子类的行为，我们不便于控制增加行为的方式和时机。</p>
</blockquote>
<ul>
<li>组合</li>
</ul>
<blockquote>
<p>组合即将一个对象嵌入到另一个对象中，由另一个对象来决定是否引用该对象来扩展自己的行为。这是一种动态的方式，我们可以在应用程序中动态的控制。<br>与继承相比，组合关系的优势就在于不会破坏类的封装性，且具有较好的松耦合性，可以使系统更加容易维护。但是它的缺点就在于要创建比继承更多的对象。</p>
</blockquote>
<h3 id="装饰者模式的优缺点"><a href="#装饰者模式的优缺点" class="headerlink" title="装饰者模式的优缺点"></a>装饰者模式的优缺点</h3><ul>
<li>优点<blockquote>
</blockquote>
</li>
</ul>
<ol>
<li>装饰者模式可以提供比继承更多的灵活性</li>
<li>可以通过一种动态的方式来扩展一个对象的功能，在运行时选择不同的装饰器，从而实现不同的行为</li>
<li>通过使用不同的具体装饰类以及这些装饰类的排列组合，可以创造出很多不同行为的组合。可以使用多个具体装饰类来装饰同一对象，得到功能更为强大的对象</li>
<li>具体构件类与具体装饰类可以独立变化，用户可以根据需要增加新的具体构件类和具体装饰类，在使用时再对其进行组合，原有代码无须改变，符合“开闭原则”</li>
</ol>
<ul>
<li>缺点</li>
</ul>
<blockquote>
</blockquote>
<ol>
<li>会产生很多的小对象，增加了系统的复杂性</li>
<li>这种比继承更加灵活机动的特性，也同时意味着装饰模式比继承更加易于出错，排错也很困难，对于多次装饰的对象，调试时寻找错误可能需要逐级排查，较为烦琐</li>
</ol>
<h3 id="装饰者的使用场景"><a href="#装饰者的使用场景" class="headerlink" title="装饰者的使用场景"></a>装饰者的使用场景</h3><ol>
<li>在不影响其他对象的情况下，以动态、透明的方式给单个对象添加职责</li>
<li>需要动态地给一个对象增加功能，这些功能也可以动态地被撤销。  当不能采用继承的方式对系统进行扩充或者采用继承不利于系统扩展和维护时</li>
</ol>
<p><img src="https://user-images.githubusercontent.com/9856659/51297117-07197b00-1a5a-11e9-940d-75765e148704.png" alt="image"></p>
<h3 id="Ex"><a href="#Ex" class="headerlink" title="Ex:"></a>Ex:</h3><p><em>装饰者基类</em><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">package com.xinye.test.decoration;</span><br><span class="line">  /**</span><br><span class="line">   * 食物基类</span><br><span class="line">   * @author xinye</span><br><span class="line">   *</span><br><span class="line">   */</span><br><span class="line">  public abstract class Food &#123;</span><br><span class="line">      protected String desc;</span><br><span class="line">     public abstract String getDesc();</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure></p>
<p>_鸡肉_<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">package com.xinye.test.decoration;</span><br><span class="line">  /**</span><br><span class="line">   * 鸡肉</span><br><span class="line">   * @author xinye</span><br><span class="line">   */</span><br><span class="line">  public class Chicken extends Food &#123;</span><br><span class="line">      public Chicken()&#123;</span><br><span class="line">          desc = &quot;鸡肉&quot;;</span><br><span class="line">     &#125;</span><br><span class="line">     @Override</span><br><span class="line">     public String getDesc() &#123;</span><br><span class="line">         return desc;</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure></p>
<p>_鸭肉_<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">package com.xinye.test.decoration;</span><br><span class="line">  /**</span><br><span class="line">   * 鸭肉</span><br><span class="line">   * @author xinye</span><br><span class="line">   *</span><br><span class="line">   */</span><br><span class="line">  public class Duck extends Food &#123;</span><br><span class="line">      public Duck()&#123;</span><br><span class="line">          desc = &quot;鸭肉&quot;;</span><br><span class="line">     &#125;</span><br><span class="line">     @Override</span><br><span class="line">     public String getDesc() &#123;</span><br><span class="line">         return desc;</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure></p>
<p><em>装饰者基类</em></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">package com.xinye.test.decoration;</span><br><span class="line">  /**</span><br><span class="line">   * @author xinye</span><br><span class="line">   */</span><br><span class="line">  public abstract class FoodDecoration extends Food &#123;</span><br><span class="line">      @Override</span><br><span class="line">     public abstract String getDesc();   </span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p><em>蒸-装饰者</em></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">package com.xinye.test.decoration;</span><br><span class="line"> /**</span><br><span class="line">  * 蒸食物</span><br><span class="line">  * @author xinye</span><br><span class="line">  */</span><br><span class="line"> public class SteamedFood extends FoodDecoration &#123;</span><br><span class="line">     private Food food;</span><br><span class="line">     public SteamedFood(Food f)&#123;</span><br><span class="line">         this.food = f;</span><br><span class="line">     &#125;</span><br><span class="line">     </span><br><span class="line">     @Override</span><br><span class="line">     public String getDesc() &#123;</span><br><span class="line">         return getDecoration() + food.getDesc();</span><br><span class="line">     &#125;</span><br><span class="line">    </span><br><span class="line">     private String getDecoration()&#123;</span><br><span class="line">         return &quot;蒸&quot;;</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p><em>烤-装饰者</em><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">package com.xinye.test.decoration;</span><br><span class="line">/**</span><br><span class="line"> * 烤食物</span><br><span class="line"> */</span><br><span class="line">public class RoastFood extends FoodDecoration &#123;</span><br><span class="line">    private Food food;</span><br><span class="line">    </span><br><span class="line">    public RoastFood(Food f)&#123;</span><br><span class="line">        this.food = f;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    @Override</span><br><span class="line">    public String getDesc() &#123;</span><br><span class="line">        return getDecoration() + food.getDesc();</span><br><span class="line">   &#125;</span><br><span class="line">    </span><br><span class="line">    private String getDecoration()&#123;</span><br><span class="line">        return &quot;烤&quot;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><em>客户端</em><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">package com.xinye.test.decoration;</span><br><span class="line">/**</span><br><span class="line"> * 客户端</span><br><span class="line"> * @author xinye</span><br><span class="line"> *</span><br><span class="line"> */</span><br><span class="line">public class Client &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        // 测试单纯的食物</span><br><span class="line">        Food f1 = new Chicken();</span><br><span class="line">        System.out.println(f1.getDesc());</span><br><span class="line">        </span><br><span class="line">        System.out.println(&quot;----------------------&quot;);</span><br><span class="line">        // 测试单重修饰的食物</span><br><span class="line">        RoastFood rf = new RoastFood(f1);</span><br><span class="line">        System.out.println(rf.getDesc());</span><br><span class="line">        </span><br><span class="line">        System.out.println(&quot;----------------------&quot;);</span><br><span class="line">        // 测试多重修饰的食物</span><br><span class="line">        SteamedFood sf = new SteamedFood(rf);</span><br><span class="line">        System.out.println(sf.getDesc());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>执行结果： <br>鸡肉 ———————- <br>烤鸡肉 ———————- <br>蒸烤鸡肉</p>
</blockquote>

          
        
      
    </div>

    

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/4/"><i class="fa fa-angle-left" aria-label="上一页"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/4/">4</a><span class="page-number current">5</span><a class="page-number" href="/page/6/">6</a><a class="page-number" href="/page/7/">7</a><a class="extend next" rel="next" href="/page/6/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>



          </div>
          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <div class="site-overview-wrap sidebar-panel sidebar-panel-active">
        
       <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  
    <img class="site-author-image" itemprop="image" src="https://avatars0.githubusercontent.com/u/9856659" alt="Lip">
  
  <p class="site-author-name" itemprop="name">Lip</p>
  <div class="site-description motion-element" itemprop="description"></div>
</div>


  <nav class="site-state motion-element">
    
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">35</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    

    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/categories/">
          
        
        
        
          
        
          
        
          
        
          
        
          
        
          
        
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    

    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
        <span class="site-state-item-count">16</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>



  <div class="feed-link motion-element">
    <a href="/atom.xml" rel="alternate">
      <i class="fa fa-rss"></i>RSS
    </a>
  </div>





  <div class="links-of-author motion-element">
    
      <span class="links-of-author-item">
      
      
      
        
      
        <a href="/13126666448" title="WeChat &rarr; 13126666448"><i class="fa fa-fw fa-wechat"></i></a>
      </span>
    
      <span class="links-of-author-item">
      
      
      
        
      
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dkeTMzMTY0NDc0MQ==" title="GitHub &rarr; https://github.com/wdy331644741"><i class="fa fa-fw fa-github"></i></span>
      </span>
    
      <span class="links-of-author-item">
      
      
      
        
      
        <span class="exturl" data-url="bWFpbHRvOjEzMDE1NTMxNzE3QDE2My5jb20=" title="E-Mail &rarr; mailto:13015531717@163.com"><i class="fa fa-fw fa-envelope"></i></span>
      </span>
    
      <span class="links-of-author-item">
      
      
      
        
      
        <span class="exturl" data-url="aHR0cHM6Ly93ZWliby5jb20vZWNob3RvdG82NjY=" title="Weibo &rarr; https://weibo.com/echototo666"><i class="fa fa-fw fa-weibo"></i></span>
      </span>
    
      <span class="links-of-author-item">
      
      
      
        
      
        <span class="exturl" data-url="aHR0cHM6Ly93d3cuZmFjZWJvb2suY29tL2VjaG90b3RvNjY2" title="FB Page &rarr; https://www.facebook.com/echototo666"><i class="fa fa-fw fa-facebook"></i></span>
      </span>
    
      <span class="links-of-author-item">
      
      
      
        
      
        <span class="exturl" data-url="aHR0cHM6Ly9zdGFja292ZXJmbG93LmNvbS91c2Vycy85OTY3MzcxL2Rvbmd5YW5nLXdhbmc=" title="StackOverflow &rarr; https://stackoverflow.com/users/9967371/dongyang-wang"><i class="fa fa-fw fa-stack-overflow"></i></span>
      </span>
    
  </div>







          
        </div>
        
        <script type="text/javascript" charset="utf-8" src="/js/tagcloud.js"></script>
        <script type="text/javascript" charset="utf-8" src="/js/tagcanvas.js"></script>
        <div class="widget-wrap">
          <h3 class="widget-title">Tag Cloud</h3>
          <div id="myCanvasContainer" class="widget tagcloud">
            <canvas width="250" height="250" id="resCanvas" style="width=100%">
              <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/IoC/">IoC</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/api/">api</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/awk/">awk</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/c/">c</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/debug/">debug</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/">git</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/laravel/">laravel</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mac/">mac</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mysql/">mysql</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/php/">php</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/react/">react</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/">redis</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/shell/">shell</a><span class="tag-list-count">10</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/webpack/">webpack</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/设计模式/">设计模式</a><span class="tag-list-count">1</span></li></ul>
            </canvas>
          </div>
        </div>
        
      </div>

      

      
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">  <span class="exturl" data-url="aHR0cDovL3d3dy5iZWlhbi5taWl0Lmdvdi5jbg==">echototo666 </span>&copy; 2017 – <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Lip</span>

  

  
</div>











<div class="BbeiAn-info"">
  京ICP备 -
  <a href="http://beian.miit.gov.cn/">20007883号</a>
  
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="post-meta-item-icon">
      <i class="fa fa-user"></i>
    </span>
    <span class="site-uv" title="总访客量">
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  

  
    <span class="post-meta-divider">|</span>
  

  
    <span class="post-meta-item-icon">
      <i class="fa fa-eye"></i>
    </span>
    <span class="site-pv" title="总访问量">
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>









        
      </div>
    </footer>

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>










  
  















  
  <script src="/lib/jquery/index.js?v=3.4.1"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>




  <script src="/js/utils.js?v=7.2.0"></script>

  <script src="/js/motion.js?v=7.2.0"></script>



  
  


  <script src="/js/affix.js?v=7.2.0"></script>

  <script src="/js/schemes/pisces.js?v=7.2.0"></script>




  

  <script src="/js/next-boot.js?v=7.2.0"></script>

  

  
  <script src="/js/exturl.js?v=7.2.0"></script>


  

  

  

  

  

  

  



  




  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  




  



<script>
// GET RESPONSIVE HEIGHT PASSED FROM IFRAME

window.addEventListener("message", function(e) {
  var data = e.data;
  if ((typeof data === 'string') && (data.indexOf('ciu_embed') > -1)) {
    var featureID = data.split(':')[1];
    var height = data.split(':')[2];
    $(`iframe[data-feature=${featureID}]`).height(parseInt(height) + 30);
  }
}, false);
</script>


  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>


  

  


  

</body>
</html>
